package com.talent.jiwen.teacher;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.talent.jiwen.R;
import com.talent.jiwen.base.BaseActivity;
import com.talent.jiwen.http.common.ActivityLifeCycleEvent;
import com.talent.jiwen.http.common.ApiFactory;
import com.talent.jiwen.http.common.HttpUtil;
import com.talent.jiwen.http.common.ProgressSubscriber;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.talent.jiwen.util.Validators;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;

public class OrderAppealActivity extends BaseActivity {
    @BindView(R.id.serviceTv)
    TextView serviceTv;
    @BindView(R.id.edit_qus)
    EditText editQus;
    @BindView(R.id.checkLayout)
    LinearLayout checkLayout;
    @BindView(R.id.checkIv)
    ImageView checkIv;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.callTv)
    TextView callTv;
    @BindView(R.id.phoneNumberTv)
    TextView phoneNumberTv;

    private String phoneNumber = "076921454399";

    private boolean rebackCheck = false;
    private String orderId;
    private int appealType;

    public static final int APPEAL_ORDER = 1;
    public static final int APPEAL_CREADIT = 2;
    private static final String PARAM_APPEAL_TYPE = "param.appeal.type";
    private static final String PARAM_ORDER_ID = "param.order.id";

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_appeal;
    }

    @Override
    public String titleName() {
        return "申诉";
    }

    @Override
    protected void initData() {
        checkIv.setImageResource(R.mipmap.img_no_chooae);
        orderId = getIntent().getStringExtra(PARAM_ORDER_ID);
        appealType = getIntent().getIntExtra(PARAM_APPEAL_TYPE , 1);

        if (appealType == APPEAL_ORDER){
            checkLayout.setVisibility(View.VISIBLE);
        }else{
            checkLayout.setVisibility(View.GONE);
        }

        phoneNumberTv.setText(phoneNumber);
    }

    @OnClick({R.id.checkIv, R.id.submit, R.id.callTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.checkIv:
                if (rebackCheck){
                    rebackCheck = false;
                    checkIv.setImageResource(R.mipmap.img_no_chooae);
                }else{
                    rebackCheck = true;
                    checkIv.setImageResource(R.mipmap.img_choose);
                }
                break;
            case R.id.submit:

                String content = editQus.getText().toString();
                if (Validators.isEmpty(content)){
                    showToast("请输入内容");
                }else{
                    saveAppealInfo(content);
                }
                break;
            case R.id.callTv:
                callPhone(phoneNumber);
                break;
        }
    }

    /**提交申诉信息
     *
     * @param content
     */
    private void saveAppealInfo(String content){
        Map<String, String> map = new HashMap<>();
        map.put("studentId" , SPUtil.stringOut(SPConstant.USER_ID));
        map.put("orderId" , orderId);
        if (appealType == APPEAL_ORDER){
            if (rebackCheck){
                map.put("appealType" , "1");
            }else{
                map.put("appealType" , "2");
            }
        }else{
            map.put("appealType" , "3");
        }

        map.put("appealMessage" , content);


        Observable observable = ApiFactory.getEducationApiSingleton().saveAppealInfo(SPUtil.stringOut(SPConstant.TOKEN) , map);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscriber<Void>(OrderAppealActivity.this) {
            @Override
            protected void _onNext(Void v) {
                showToast("申诉已提交");
                finish();
            }

            @Override
            protected void _onError(String message) {
                showToast(message);
            }
        }, ActivityLifeCycleEvent.DESTROY, lifecycleSubject,false);
    }

    /**启动该页面
     *
     * @param context
     * @param orderId
     */
    public static void startOrderAppealActivity(Context context , int appealType , String orderId ){
        Intent intent = new Intent();
        intent.putExtra(PARAM_APPEAL_TYPE , appealType);
        intent.putExtra(PARAM_ORDER_ID , orderId);
        intent.setClass(context , OrderAppealActivity.class);
        context.startActivity(intent);
    }


    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }
}
